import router from "@/router";
export default {
    state() {
        return {
            dynamicTag: [{ path: "home", name: "home", meta: { title: "首页" } }], //现在打开的
            id: null,
        }
    },
    getters: {
        dynamicTag(state) {
            return state.dynamicTag
        }
    },
    mutations: {
        //添加标签页
        addTag(state, route) {
            if (route.name == "reset" || route.name == "home" || route.name == "Login") {
                return;
            }
            // 查找
            let findTag = state.dynamicTag.find((item) => item.name == route.name);
            if (!findTag) {
                if (route.name == "addProductMessage" || route.name == "compileProductMessage") {
                    let index = state.dynamicTag.findIndex((p) => p.name == "productmessage");
                    if (index > -1) {
                        state.dynamicTag.splice(index + 1, 0, {
                            meta: route.meta,
                            title: route.meta?.title,
                            name: route.name,
                            path: route.path,
                        })
                        router.push({ path: route.path });
                        return
                    }
                }
                state.dynamicTag = [
                    ...state.dynamicTag,
                    { title: route.meta.title, meta: route.meta, name: route.name, path: route.path },
                ];
                router.push(route.path);
            }
        },
        //删除标签页
        closeTag(state, index) {
            if (!Number(index)) {
                index = state.dynamicTag.findIndex((item) => item.name == index);
            }
            let route = router.currentRoute.value;
            if (state.dynamicTag[index].title == "首页") {
                return;
            }
            if (route.name == state.dynamicTag[index].name) {
                const { name, params, query } = state.dynamicTag[index + 1] || state.dynamicTag[index - 1]
                router.push({ name, params, query })
            }
            if (route.name == "addProductMessage" || route.name == "compileProductMessage") {
                router.push({ name: "productmessage" });
            }
            state.dynamicTag.splice(index, 1);
        },
        //点击跳转
        tabRouter(state, tag) {
            if (tag == "compileProductMessage") {
                router.push({
                    name: tag,
                    query: { id: state.id }
                })
            } else {
                router.push({
                    name: tag,
                })
            }

        },
        //刷新
        refResh(state, e) {
            // 跳转路由点击
            router.push({
                path: `/Index/reset/${e.path}`,
            });
        },
        //删除右边的标签
        deleteRight(state, tag) {
            state.dynamicTag.splice(tag.index + 1, state.dynamicTag.length - tag.index);
            router.push({
                name: state.dynamicTag[tag.index].name,
            });
        },
        //删除全部标签
        deletAll(state) {
            state.dynamicTag.splice(1, state.dynamicTag.length)
            router.replace({ path: "/Index/home" }); //跳转到首页
        },
        getId(state, id) {
            state.id = id;
        },
    },
    actions: {
        //添加标签页
        addTag(context, route) {
            context.commit("addTag", route)
        },
        //删除标签页
        closeTag(context, index) {
            context.commit("closeTag", index)
        },
        //点击跳转
        tabRouter(context, index) {
            context.commit("tabRouter", index)
        },
        //刷新
        refResh(context, e) {
            context.commit("context", e)
        },
        //关闭右侧标签
        deleteRight(context, tag) {
            context.commit("deleteRight", tag)
        },
        //删除全部标签
        deletAll(context) {
            context.commit("deletAll")
        },
        getId(context, id) {
            context.commit("getId", id)
        },
    },
};
